<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>mainwindow.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-tools-plugandpaint-app-example.html">Plug &amp; Paint Example</a></td><td >mainwindow.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">mainwindow.cpp 示例文件</h1>
<span class="subtitle">tools/plugandpaint/app/mainwindow.cpp</span>
<!-- $$$tools/plugandpaint/app/mainwindow.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;interfaces.h&quot;</span>
  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>
  <span class="preprocessor">#include &quot;paintarea.h&quot;</span>
  <span class="preprocessor">#include &quot;plugindialog.h&quot;</span>

  <span class="preprocessor">#include &lt;QAction&gt;</span>
  <span class="preprocessor">#include &lt;QActionGroup&gt;</span>
  <span class="preprocessor">#include &lt;QApplication&gt;</span>
  <span class="preprocessor">#include &lt;QColorDialog&gt;</span>
  <span class="preprocessor">#include &lt;QFileDialog&gt;</span>
  <span class="preprocessor">#include &lt;QInputDialog&gt;</span>
  <span class="preprocessor">#include &lt;QMenu&gt;</span>
  <span class="preprocessor">#include &lt;QMenuBar&gt;</span>
  <span class="preprocessor">#include &lt;QMessageBox&gt;</span>
  <span class="preprocessor">#include &lt;QPluginLoader&gt;</span>
  <span class="preprocessor">#include &lt;QScrollArea&gt;</span>
  <span class="preprocessor">#include &lt;QTimer&gt;</span>

  MainWindow<span class="operator">::</span>MainWindow() :
      paintArea(<span class="keyword">new</span> PaintArea)<span class="operator">,</span>
      scrollArea(<span class="keyword">new</span> <span class="type"><a href="qscrollarea.html">QScrollArea</a></span>)
  {
      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Dark);
      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setWidget(paintArea);
      setCentralWidget(scrollArea);

      createActions();
      createMenus();
      loadPlugins();

      setWindowTitle(tr(<span class="string">&quot;Plug &amp; Paint&quot;</span>));

      <span class="keyword">if</span> (<span class="operator">!</span>brushActionGroup<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>isEmpty())
          brushActionGroup<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>first()<span class="operator">-</span><span class="operator">&gt;</span>trigger();

      <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span><span class="operator">::</span>singleShot(<span class="number">500</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>aboutPlugins);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>open()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getOpenFileName(<span class="keyword">this</span><span class="operator">,</span>
                                                            tr(<span class="string">&quot;Open File&quot;</span>)<span class="operator">,</span>
                                                            <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath());
      <span class="keyword">if</span> (<span class="operator">!</span>fileName<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>paintArea<span class="operator">-</span><span class="operator">&gt;</span>openImage(fileName)) {
              <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Plug &amp; Paint&quot;</span>)<span class="operator">,</span>
                                       tr(<span class="string">&quot;Cannot load %1.&quot;</span>)<span class="operator">.</span>arg(fileName));
              <span class="keyword">return</span>;
          }
          paintArea<span class="operator">-</span><span class="operator">&gt;</span>adjustSize();
      }
  }

  bool MainWindow<span class="operator">::</span>saveAs()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> initialPath <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath() <span class="operator">+</span> <span class="string">&quot;/untitled.png&quot;</span>;

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Save As&quot;</span>)<span class="operator">,</span>
                                                            initialPath);
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span> <span class="keyword">false</span>;

      <span class="keyword">return</span> paintArea<span class="operator">-</span><span class="operator">&gt;</span>saveImage(fileName<span class="operator">,</span> <span class="string">&quot;png&quot;</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>brushColor()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> newColor <span class="operator">=</span> <span class="type"><a href="qcolordialog.html">QColorDialog</a></span><span class="operator">::</span>getColor(paintArea<span class="operator">-</span><span class="operator">&gt;</span>brushColor());
      <span class="keyword">if</span> (newColor<span class="operator">.</span>isValid())
          paintArea<span class="operator">-</span><span class="operator">&gt;</span>setBrushColor(newColor);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>brushWidth()
  {
      bool ok;
      <span class="keyword">const</span> <span class="type">int</span> newWidth <span class="operator">=</span> <span class="type"><a href="qinputdialog.html">QInputDialog</a></span><span class="operator">::</span>getInt(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Plug &amp; Paint&quot;</span>)<span class="operator">,</span>
                                                tr(<span class="string">&quot;Select brush width:&quot;</span>)<span class="operator">,</span>
                                                paintArea<span class="operator">-</span><span class="operator">&gt;</span>brushWidth()<span class="operator">,</span>
                                                <span class="number">1</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="operator">&amp;</span>ok);
      <span class="keyword">if</span> (ok)
          paintArea<span class="operator">-</span><span class="operator">&gt;</span>setBrushWidth(newWidth);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>changeBrush()
  {
      <span class="keyword">auto</span> action <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="keyword">auto</span> iBrush <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>BrushInterface <span class="operator">*</span><span class="operator">&gt;</span>(action<span class="operator">-</span><span class="operator">&gt;</span>parent());
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> brush <span class="operator">=</span> action<span class="operator">-</span><span class="operator">&gt;</span>text();

      paintArea<span class="operator">-</span><span class="operator">&gt;</span>setBrush(iBrush<span class="operator">,</span> brush);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>insertShape()
  {
      <span class="keyword">auto</span> action <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="keyword">auto</span> iShape <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>ShapeInterface <span class="operator">*</span><span class="operator">&gt;</span>(action<span class="operator">-</span><span class="operator">&gt;</span>parent());

      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> path <span class="operator">=</span> iShape<span class="operator">-</span><span class="operator">&gt;</span>generateShape(action<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">,</span> <span class="keyword">this</span>);
      <span class="keyword">if</span> (<span class="operator">!</span>path<span class="operator">.</span>isEmpty())
          paintArea<span class="operator">-</span><span class="operator">&gt;</span>insertShape(path);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>applyFilter()
  {
      <span class="keyword">auto</span> action <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="keyword">auto</span> iFilter <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>FilterInterface <span class="operator">*</span><span class="operator">&gt;</span>(action<span class="operator">-</span><span class="operator">&gt;</span>parent());

      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> image <span class="operator">=</span> iFilter<span class="operator">-</span><span class="operator">&gt;</span>filterImage(action<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">,</span> paintArea<span class="operator">-</span><span class="operator">&gt;</span>image()<span class="operator">,</span>
                                                <span class="keyword">this</span>);
      paintArea<span class="operator">-</span><span class="operator">&gt;</span>setImage(image);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>about()
  {
     <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;About Plug &amp; Paint&quot;</span>)<span class="operator">,</span>
              tr(<span class="string">&quot;The &lt;b&gt;Plug &amp; Paint&lt;/b&gt; example demonstrates how to write Qt &quot;</span>
                 <span class="string">&quot;applications that can be extended through plugins.&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>aboutPlugins()
  {
      PluginDialog dialog(pluginsDir<span class="operator">.</span>path()<span class="operator">,</span> pluginFileNames<span class="operator">,</span> <span class="keyword">this</span>);
      dialog<span class="operator">.</span>exec();
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createActions()
  {
      openAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Open...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      openAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Open);
      connect(openAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>open);

      saveAsAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Save As...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      saveAsAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>SaveAs);
      connect(saveAsAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>saveAs);

      exitAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      exitAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit);
      connect(exitAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>close);

      brushColorAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Brush Color...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(brushColorAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>brushColor);

      brushWidthAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Brush Width...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(brushWidthAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>brushWidth);

      brushActionGroup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);

      aboutAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;About&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(aboutAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>about);

      aboutQtAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;About &amp;Qt&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(aboutQtAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <a href="qapplication.html#qApp">qApp</a><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>aboutQt);

      aboutPluginsAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;About &amp;Plugins&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(aboutPluginsAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>aboutPlugins);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createMenus()
  {
      fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(openAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(saveAsAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(exitAct);

      brushMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Brush&quot;</span>));
      brushMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(brushColorAct);
      brushMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(brushWidthAct);
      brushMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      shapesMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Shapes&quot;</span>));

      filterMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Filter&quot;</span>));

      menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      helpMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Help&quot;</span>));
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(aboutAct);
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(aboutQtAct);
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(aboutPluginsAct);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>loadPlugins()
  {
      <span class="keyword">const</span> <span class="keyword">auto</span> staticInstances <span class="operator">=</span> <span class="type"><a href="../qtcore/qpluginloader.html">QPluginLoader</a></span><span class="operator">::</span>staticInstances();
      <span class="keyword">for</span> (<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>plugin : staticInstances)
          populateMenus(plugin);

      pluginsDir <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span>(<a href="qapplication.html#qApp">qApp</a><span class="operator">-</span><span class="operator">&gt;</span>applicationDirPath());

  <span class="preprocessor">#if defined(Q_OS_WIN)</span>
      <span class="keyword">if</span> (pluginsDir<span class="operator">.</span>dirName()<span class="operator">.</span>toLower() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;debug&quot;</span> <span class="operator">|</span><span class="operator">|</span> pluginsDir<span class="operator">.</span>dirName()<span class="operator">.</span>toLower() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;release&quot;</span>)
          pluginsDir<span class="operator">.</span>cdUp();
  <span class="preprocessor">#elif defined(Q_OS_MAC)</span>
      <span class="keyword">if</span> (pluginsDir<span class="operator">.</span>dirName() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;MacOS&quot;</span>) {
          pluginsDir<span class="operator">.</span>cdUp();
          pluginsDir<span class="operator">.</span>cdUp();
          pluginsDir<span class="operator">.</span>cdUp();
      }
  <span class="preprocessor">#endif</span>
      pluginsDir<span class="operator">.</span>cd(<span class="string">&quot;plugins&quot;</span>);

      <span class="keyword">const</span> <span class="keyword">auto</span> entryList <span class="operator">=</span> pluginsDir<span class="operator">.</span>entryList(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>Files);
      <span class="keyword">for</span> (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName : entryList) {
          <span class="type"><a href="../qtcore/qpluginloader.html">QPluginLoader</a></span> loader(pluginsDir<span class="operator">.</span>absoluteFilePath(fileName));
          <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>plugin <span class="operator">=</span> loader<span class="operator">.</span>instance();
          <span class="keyword">if</span> (plugin) {
              populateMenus(plugin);
              pluginFileNames <span class="operator">+</span><span class="operator">=</span> fileName;
          }
      }

      brushMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>brushActionGroup<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>isEmpty());
      shapesMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>shapesMenu<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>isEmpty());
      filterMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>filterMenu<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>isEmpty());
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>populateMenus(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>plugin)
  {
      <span class="keyword">auto</span> iBrush <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>BrushInterface <span class="operator">*</span><span class="operator">&gt;</span>(plugin);
      <span class="keyword">if</span> (iBrush)
          addToMenu(plugin<span class="operator">,</span> iBrush<span class="operator">-</span><span class="operator">&gt;</span>brushes()<span class="operator">,</span> brushMenu<span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>changeBrush<span class="operator">,</span>
                    brushActionGroup);

      <span class="keyword">auto</span> iShape <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>ShapeInterface <span class="operator">*</span><span class="operator">&gt;</span>(plugin);
      <span class="keyword">if</span> (iShape)
          addToMenu(plugin<span class="operator">,</span> iShape<span class="operator">-</span><span class="operator">&gt;</span>shapes()<span class="operator">,</span> shapesMenu<span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>insertShape);

      <span class="keyword">auto</span> iFilter <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>FilterInterface <span class="operator">*</span><span class="operator">&gt;</span>(plugin);
      <span class="keyword">if</span> (iFilter)
          addToMenu(plugin<span class="operator">,</span> iFilter<span class="operator">-</span><span class="operator">&gt;</span>filters()<span class="operator">,</span> filterMenu<span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>applyFilter);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>addToMenu(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>plugin<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>texts<span class="operator">,</span>
                             <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>menu<span class="operator">,</span> Member member<span class="operator">,</span>
                             <span class="type"><a href="qactiongroup.html">QActionGroup</a></span> <span class="operator">*</span>actionGroup)
  {
      <span class="keyword">for</span> (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text : texts) {
          <span class="keyword">auto</span> action <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(text<span class="operator">,</span> plugin);
          connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> member);
          menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(action);

          <span class="keyword">if</span> (actionGroup) {
              action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
              actionGroup<span class="operator">-</span><span class="operator">&gt;</span>addAction(action);
          }
      }
  }

</pre>
</div>
<!-- @@@tools/plugandpaint/app/mainwindow.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
